package com.smit.livevideo.sample.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.smit.livevideo.sample.db.SqliteBaseUtil;
import com.smit.livevideo.sample.db.Tables;
import com.smit.utils.LogUtil;
import com.smit.utils.StrUtil;

/**
 * Created by smfang on 2015/11/24.
 * 处理数据库中频道的接口类
 */
public final class ChannelDAO
{
    private static final String TAG = ChannelDAO.class.getSimpleName();
    private String dvbChannelTableName = Tables.DVBChannel.DVB_CHANNEL_TABLE_NAME;
    private SqliteBaseUtil sqliteBaseUtil;
    private SQLiteDatabase database;

    private ChannelDAO()
    {
        sqliteBaseUtil = SqliteBaseUtil.getInstace();
        database = sqliteBaseUtil.getDbHelper().getReadableDatabase();
    }

    /**
     * 获取单例对象
     * @return ChannelDAO
     */
    public static ChannelDAO getInstance()
    {
        return SingleTonHolder.instance;
    }

    /**
     * 查询全部频道列表
     * @return List<Map<String, String>>
     */
    public List<Map<String, String>> queryAllChannelList()
    {
        return sqliteBaseUtil.queryAll(dvbChannelTableName, Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX, false);

    }

    /**
     * 每次搜完台或者重新搜台将数据同步到数据库
     * @param allChannelList 所有频道列表的list
     * @return int
     */
    public int initAllChannelList(List<ContentValues> allChannelList)
    {
        if (allChannelList == null || allChannelList.size() == 0)
        {
            LogUtil.trace(TAG, "allChannelList == null || allChannelList.size() == 0");
            return -1;
        }
        LogUtil.trace(TAG, "initAllChannelList allChannelList=" + allChannelList.toString());
        sqliteBaseUtil.deleteAll(dvbChannelTableName);

        try
        {
            database.beginTransaction();
            for (ContentValues values : allChannelList)
            {
                long i = sqliteBaseUtil.insert(dvbChannelTableName, values);
            }
            database.setTransactionSuccessful();
        }
        catch (Exception e)
        {
            LogUtil.trace(TAG, "initAllChannelList Exception");
        }
        finally
        {
            LogUtil.trace(TAG, "initAllChannelList finally");
            database.endTransaction();
            //databaseHelper.getReadableDatabase().close();//单例模式，不需要close，可能其他的地方在用（更新频道别名出问题了）
        }
        return 1;
    }

    /**
     * 获取当前频道列表
     * @return List<Map<String, String>>
     */
    public List<Map<String, String>> getCurrentChannelList()
    {
        return queryAllChannelList();
    }

    /**
     * 获取当前类型的频道名称列表
     * @return List<String>
     */
    public List<String> getCurrentChannelNameList()
    {
        List<Map<String, String>> list;
        List<String> resultList = new ArrayList<String>();
        list = queryAllChannelList();
        for (int i = 0; i < list.size(); i++)
        {
            Map<String, String> map = list.get(i);
            resultList.add(map.get(Tables.DVBChannel.DVB_CHANNEL_FIELD_CHANNELNAME));
        }
        return resultList;
    }

    /**
     * 获取当前类型的频道索引列表
     * @return List<Integer>
     */
    public List<Integer> getCurrentChannelIndexList()
    {
        List<Map<String, String>> list;
        List<Integer> resultList = new ArrayList<Integer>();
        list = queryAllChannelList();
        for (int i = 0; i < list.size(); i++)
        {
            Map<String, String> map = list.get(i);
            resultList.add(StrUtil.parseInt(map.get(Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX)));
        }
        return resultList;
    }

    /**
     * getIndexByChannelName
     * @param channelName 需要获取index的频道名称
     * @return int
     */
    public int getIndexByChannelName(String channelName)
    {
        List<Map<String, String>> list;
        Map<String, String> whereMap = new HashMap<String, String>();
        whereMap.put(Tables.DVBChannel.DVB_CHANNEL_FIELD_CHANNELNAME, channelName);
        list = sqliteBaseUtil.query(dvbChannelTableName, whereMap, Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX, false);
        if (list != null && list.size() > 0)
        {
            return StrUtil.parseInt(list.get(0).get(Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX));
        }
        return -1;
    }

    /**
     * 根据频道号查找频道索引
     * @param num 需要获取频道index的逻辑频道号
     * @return int
     */
    public int getIndexByChannelNum(int num)
    {
        List<Map<String, String>> list;
        Map<String, String> whereMap = new HashMap<String, String>();
        whereMap.put(Tables.DVBChannel.DVB_CHANNEL_FIELD_CHANNELNUMBER, num + "");
        list = sqliteBaseUtil.query(dvbChannelTableName, whereMap, Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX, false);

        if (list != null && list.size() > 0)
        {
            return StrUtil.parseInt(list.get(0).get(Tables.DVBChannel.DVB_CHANNEL_FIELD_INDEX));
        }
        return -1;
    }

    private static class SingleTonHolder
    {
        private static ChannelDAO instance = new ChannelDAO();
    }
}
